package handles

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"repo-master/cache"
	"repo-master/models"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {
	repo_code := c.PostForm("repo_code")
	repo_name := c.PostForm("repo_name")
	repo_address := c.PostForm("repo_address")

	data := models.Repo{

		RepoCode:    repo_code,
		RepoName:    repo_name,
		RepoAddress: repo_address,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := models.Order{

		OrderCode:  order_code,
		OrderNum:   order_num,
		OrderPrice: order_price,
		Rid:        rid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	/*
		SELECT
		  orders.id AS OrderId,
		  repos.repo_code AS RepoCode,
		  repos.repo_name AS RepoName,
		  repos.repo_address AS RepoAddress,
		  orders.order_code AS OrderCode,
		  orders.order_num AS OrderNum,
		  orders.order_price AS OrderPrice
		FROM
		  repos
		  LEFT JOIN orders ON repos.id = orders.rid
	*/
	var order []models.RepoOrder
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("repos").
			Select("orders.id AS OrderId",
				"repos.repo_code AS RepoCode",
				"repos.repo_name AS RepoName",
				"repos.repo_address AS RepoAddress",
				"orders.order_code AS OrderCode",
				"orders.order_num AS OrderNum",
				"orders.order_price AS OrderPrice",
			).Joins("LEFT JOIN orders ON repos.id = orders.rid").
			Offset(offset).Limit(size).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}

	var total int64
	models.DB.Model(&models.Repo{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  order,
		"total": total,
	})
	return

}

func RepoOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  orders.id AS OrderId,
		  repos.repo_code AS RepoCode,
		  repos.repo_name AS RepoName,
		  repos.repo_address AS RepoAddress,
		  orders.order_code AS OrderCode,
		  orders.order_num AS OrderNum,
		  orders.order_price AS OrderPrice
		FROM
		  repos
		  LEFT JOIN orders ON repos.id = orders.rid
	*/
	var order models.RepoOrder
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("repos").
			Select("orders.id AS OrderId",
				"repos.repo_code AS RepoCode",
				"repos.repo_name AS RepoName",
				"repos.repo_address AS RepoAddress",
				"orders.order_code AS OrderCode",
				"orders.order_num AS OrderNum",
				"orders.order_price AS OrderPrice",
			).Joins("LEFT JOIN orders ON repos.id = orders.rid").
			Where("orders.rid=?", id).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": order,
	})
	return

}
